$(function () {

    $('.cannel').on('click', function () {
        location.href = './address.html'
    })


    $('.add-address').on('click', '#dq', function () {
        $('.long').show()
        $.ajax({
            url: "/api/region/list",
            method: "GET",

            data: {
                parentId: 1
            },
            success: function (res) {
                // console.log(res);
                var htmlStr = template('tale-sheng', res)
                $('.sheng').html(htmlStr)
            }
        })
    })
    $('i').on('click', function () {
        $('.long').hide()
    })



    $('.sheng ').on('click', 'li', function () {
        // console.log('ok');
        var shegid = $(this).attr('bata-id')
        // console.log(shegid);
        localStorage.setItem('province_id', shegid)
        $('.diqu_list ul li')[0].innerHTML = `${$(this)[0].innerHTML}`
        $('.diqu_list ul li')[1].click()

        $.ajax({
            url: "/api/region/list",
            method: "GET",

            data: {
                parentId: shegid
            },
            success: function (res) {
                // console.log(res);
                var htmlStr = template('tale-shi', res)
                $('.shi').html(htmlStr)
            }
        })

    })


    $('.shi ').on('click', 'li', function () {

        var sheiid = $(this).attr('bata-id')
        // console.log(sheiid);
        localStorage.setItem('city_id', sheiid)

        $('.diqu_list ul li')[1].innerHTML = `${$(this)[0].innerHTML}`
        $('.diqu_list ul li')[2].click()

        $.ajax({
            url: "/api/region/list",
            method: "GET",

            data: {
                parentId: sheiid
            },
            success: function (res) {
                // console.log(res);
                var htmlStr = template('tale-xian', res)
                $('.xian').html(htmlStr)
            }
        })
    })

    $('.xian ').on('click', 'li', function () {
        var xianid = $(this).attr('bata-id')
        localStorage.setItem('district_id', xianid)

        $('.diqu_list ul li')[2].innerHTML = `${$(this)[0].innerHTML}`
        var htmls = `${$('.diqu_list ul li')[0].innerHTML}${$('.diqu_list ul li')[1].innerHTML}${$('.diqu_list ul li')[2].innerHTML} `
        // console.log(htmls);
        $('.long').hide()
        $('#dq').val(htmls)
    })


    var faln = 1
    $('.form-default span').on('click', function () {
        console.log('ok');
        if (faln == 1) {
            $('.form-default span').removeClass('selected')
            faln = 0

        } else {
            $('.form-default span').addClass('selected')
            faln = 1
        }
    })
    // console.log(faln);

    $('.save').on('click', function () {
        var data = {
            address: $('#address').val(),
            city_id: localStorage.getItem('city_id'),
            district_id: localStorage.getItem('district_id'),
            is_default: 0,
            name: $('#name').val(),
            mobile: $('#mobile').val(),
            province_id: localStorage.getItem('province_id'),
        }
        console.log(data);

        function sub(data) {
            $.ajax({
                url: "/api/address/save",
                method: "POST",

                data,
                success: function (res) {
                    console.log(res);
                }
            });
        }
        sub(data)
        localStorage.removeItem('province_id');
        localStorage.removeItem('city_id');
        localStorage.removeItem('district_id');
        location.href = './address.html'

    })











})